package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryPowerListReq;
import com.sg.dto.base.framework.req.QueryPowerDetailReq;
import com.sg.dto.base.framework.req.QueryPowerListReq;
import com.sg.entity.Privacy;
import com.sg.service.base.framework.MPrivacyService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.PrivacyMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MPrivacyServiceImpl
        extends ServiceImpl<PrivacyMapper, Privacy>
        implements MPrivacyService {

    @Autowired
    PrivacyMapper privacyMapper;

    /**
     * 1-2-11批量查询权限列表[1245]
     * gen by moon at 10/23/2022, 2:26:01 PM
     */
    @Trace(operationName = "1-2-11批量查询权限列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Privacy> batchQueryPowerList(BatchQueryPowerListReq reqDto) {

        try {
            QueryWrapper<Privacy> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getPowerIdList() == null || reqDto.getPowerIdList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("privacy_id", reqDto.getPowerIdList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Privacy.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Privacy.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Privacy.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Privacy>) super.list(queryWrapper)).stream().map(item -> {
                item.setPrivacyLogo(CommonFunctionHelper.getFilePath(item.getPrivacyLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-11查询权限列表[121]
     * gen by moon at 10/23/2022, 2:26:01 PM
     */
    @Trace(operationName = "1-2-11查询权限列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Privacy> queryPowerList(QueryPowerListReq reqDto) {

        try {
            QueryWrapper<Privacy> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Privacy.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Privacy.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Privacy.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<Privacy>) super.list(queryWrapper)).stream().map(item -> {
                item.setPrivacyLogo(CommonFunctionHelper.getFilePath(item.getPrivacyLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-11查询权限详情[120]
     * gen by moon at 4/26/2023, 12:19:42 AM
     */
    @Trace(operationName = "1-2-11查询权限详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Privacy queryPowerDetail(QueryPowerDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<Privacy> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Privacy> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                Privacy retData = retList.get(0);
                retData.setPrivacyLogo(CommonFunctionHelper.getFilePath(retData.getPrivacyLogo()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-11删除权限[119]
     * gen by moon at 11/8/2022, 7:37:25 PM
     */
    @Trace(operationName = "1-2-11删除权限")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePower(String privacyId) {

        return super.removeById(privacyId);
    }

    /**
     * 1-2-11修改权限[118]
     * gen by moon at 11/8/2022, 7:37:24 PM
     */
    @Trace(operationName = "1-2-11修改权限")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePower(Privacy privacy) {
//     // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(privacy.getPrivacyLogo())){
//         privacy.setPrivacyLogo(CommonFunctionHelper.replaceFilePath(privacy.getPrivacyLogo()));
//     }
        privacy.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        privacy.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        privacy.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (privacy != null && privacy.getSubjectLifeCycle() != null && privacy.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacy.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(privacy);
    }

    /**
     * 1-2-11新增权限[117]
     * gen by moon at 10/23/2022, 2:25:58 PM
     */
    @Trace(operationName = "1-2-11新增权限")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPower(Privacy privacy) {
//     // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(privacy.getPrivacyLogo())){
//         privacy.setPrivacyLogo(CommonFunctionHelper.replaceFilePath(privacy.getPrivacyLogo()));
//     }
        privacy.setPrivacyId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        privacy.setIsArchive("FALSE");
        privacy.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (privacy != null && privacy.getSubjectLifeCycle() != null && privacy.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacy.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        privacy.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        privacy.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        privacy.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (privacy != null && privacy.getSubjectLifeCycle() != null && privacy.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacy.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        privacy.setIsValid("FALSE");
        privacy.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        privacy.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(privacy)) {
            return privacy.getPrivacyId();
        } else {
            return "-1";
        }

    }
}
